package com.koron.operation.classes.schedule.bean.converter;

import cn.hutool.core.util.StrUtil;
import com.koron.operation.classes.info.bean.po.ClassesItem;
import com.koron.operation.classes.schedule.bean.dto.ClassesShiftDTO;
import com.koron.operation.classes.schedule.bean.dto.ScheduleDTO;
import com.koron.operation.classes.schedule.bean.po.ClassesStaff;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jzp
 * @since 2021/12/30
 */
public class ScheduleConverter {

    public static String ClassesShiftDTO2StaffStr(ClassesShiftDTO classesShiftDTO) {
        StringBuilder str = new StringBuilder();
        List<ClassesStaff> staffs = classesShiftDTO.getStaffs();
        if (CollectionUtils.isEmpty(staffs)) {
            return "";
        }
        for (ClassesStaff staff : staffs) {
            str.append(staff.getStaffName());
            if ("0".equals(staff.getType())) {
                str.append("|");
            } else {
                str.append(",");
            }
        }
        return StrUtil.strip(str, ",");
    }

    public static List<List<String>> getScheduleHead(ScheduleDTO scheduleDTO) {
        List<List<String>> list = new ArrayList<>();
        for (ClassesItem item : scheduleDTO.getItems()) {
            String startTime = DateFormatUtils.format(item.getStartTime(), "HH:mm");
            String endTime = DateFormatUtils.format(item.getEndTime(), "HH:mm");
            String format = String.format("%s（%s-%s）", item.getName(), startTime, endTime);
            switch (scheduleDTO.getStaffType()) {
                // 如果是0 值班长+值班员
                case "0":
                    list.add(Arrays.asList(format, "值班长"));
                    list.add(Arrays.asList(format, "值班员"));
                    break;
                // 如果是1 值守
                case "1":
                    list.add(Arrays.asList(format, "值守"));
                    break;
                // 如果是2 值班员
                case "2":
                    list.add(Arrays.asList(format, "值班员"));
                    break;
                // 如果是3 OnCall
                case "3":
                    list.add(Arrays.asList(format, "值班领导"));
                    list.add(Arrays.asList(format, "值班人员"));
                    break;
            }
        }
        return list;
    }

    public static List<List<String>> getScheduleData(ScheduleDTO scheduleDTO) {
        List<List<String>> lists = new ArrayList<>();
        ClassesShiftDTO classesShiftDTO;
        for (List<ClassesShiftDTO> classesShiftDTOS : scheduleDTO.getShiftList()) {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < classesShiftDTOS.size(); i++) {
                classesShiftDTO = classesShiftDTOS.get(i);
                if (i == 0) {
                    list.add(DateFormatUtils.format(classesShiftDTO.getStartTime(), "yyyy-MM-dd"));
                }
                List<ClassesStaff> staffs = classesShiftDTO.getStaffs();
                switch (scheduleDTO.getStaffType()) {
                    // 如果是0 值班长+值班员
                    case "0":
                        // 值班长
                        list.add(staffs.stream()
                                .filter(e -> "0".equals(e.getType()))
                                .map(ClassesStaff::getStaffName)
                                .collect(Collectors.joining("，")));
                        // 值班员
                        list.add(staffs.stream()
                                .filter(e -> "2".equals(e.getType()))
                                .map(ClassesStaff::getStaffName)
                                .collect(Collectors.joining("，")));
                        break;
                    // 如果是值守，值班员
                    case "1":
                    case "2":
                        list.add(staffs.stream()
                                .map(ClassesStaff::getStaffName)
                                .collect(Collectors.joining("，")));
                        break;
                    // 如果是3 OnCall
                    case "3":
                        // 值班领导
                        list.add(staffs.stream()
                                .filter(e -> "3".equals(e.getType()))
                                .map(ClassesStaff::getStaffName)
                                .collect(Collectors.joining("，")));
                        // 值班人员
                        list.add(staffs.stream()
                                .filter(e -> "4".equals(e.getType()))
                                .map(ClassesStaff::getStaffName)
                                .collect(Collectors.joining("，")));
                        break;
                }
            }
            lists.add(list);
        }
        return lists;
    }

    public static List<List<String>> getScheduleExcelReadMe() {
        return Arrays.asList(
                Collections.singletonList("填写说明"),
                Collections.singletonList("1、值班开始时间为首班开始日期，日期格式为yyyy/MM/dd"),
                Collections.singletonList("2、每个班次的值班长和值班员不能同时为空"),
                Collections.singletonList("3、多个人员之间用逗号分隔（中文逗号或英文逗号都可以）")
        );
    }

}
